﻿using MediatR;
using RBAC_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using System.Security;
using THE_WIND_RISES.Common.Domans.RBAC;
using THE_WIND_RISES.Common.Dtos;

namespace RBAC_Get.API.Application.Commands
{
    /// <summary>
    /// 获取所有角色信息绑定下拉框
    /// </summary>
    public class ShowRoleHandler : IRequestHandler<ShowRoleCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        public IBaseRepositoryGet<Role> RoleRep;
        ILogger<ShowRoleHandler> logger;

        public ShowRoleHandler(IBaseRepositoryGet<Role> roleRep, ILogger<ShowRoleHandler> logger)
        {
            RoleRep = roleRep;
            this.logger = logger;
        }

        public async Task<Result> Handle(ShowRoleCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Added };
            try
            {
                string ShowSql = $"select * from Role";
                var res = RoleRep.GetAll(ShowSql);
                if (res != null)
                {
                    result.Code = (int)CommonErrorCodes.Search;
                    result.Data = res;
                    result.Msg = "查询成功";
                    logger.LogInformation("获取所有角色信息绑定下拉框成功");
                }
            }
            catch (Exception)
            {
                logger.LogError("获取所有角色信息绑定下拉框失败");
                throw;
            }
            return result;
        }
    }
}
